package com.homer.gps.track;

import java.util.Locale;

import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.view.Gravity;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockPreferenceActivity;
import com.actionbarsherlock.view.MenuItem;
import com.homer.gps.track.helper.LocaleDelegate;
import com.homer.gps.track.helper.UsefullStaff;

public class Settings extends SherlockPreferenceActivity{

	private Preference showGPSSettings;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Locale preferenceLocale = LocaleDelegate.getPreferenceLocale(this);
		// if the locale has not been stored in the preferences ...
		if (preferenceLocale == null) {
			// store the default preference locale
			preferenceLocale = Locale.getDefault();
			preferenceLocale = LocaleDelegate.getAcceptedLocale(this, preferenceLocale);
			LocaleDelegate.setPreferenceLocale(this, preferenceLocale);
		}
		// update configuration to locale (if needed)
		LocaleDelegate.updateConfiguration(this, preferenceLocale);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.route_reports_settings);

		addPreferencesFromResource(R.xml.preferences);
		initActivity();
		showGPSSettings = findPreference(getString(R.string.key_location_settings));
		showGPSSettings.setOnPreferenceClickListener(new OnPreferenceClickListener() {

			public boolean onPreferenceClick(Preference preference) {
				startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
				return false;
			}
		});

		Preference languagePreference = findPreference("locale");
		languagePreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

			/*
			 * (non-Javadoc)
			 * 
			 * @see android.preference.Preference.OnPreferenceChangeListener
			 * #onPreferenceChange(android.preference.Preference,
			 * java.lang.Object)
			 */
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				String localeId = (String) newValue;
				Locale locale = LocaleDelegate.createLocaleFromId(localeId);
				boolean isPreferenceLocale = LocaleDelegate.isPreferenceLocale(Settings.this, locale);
				if (!isPreferenceLocale) {
					LocaleDelegate.setPreferenceLocale(Settings.this, locale);
					LocaleDelegate.updateConfiguration(Settings.this, locale);
					UsefullStaff.restart(Settings.this, null);
					return true;
				} else {
					return false;
				}
			}
		});
	}

	private void initActivity() {
		final ActionBar bar = getSupportActionBar();

		ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
		bar.setTitle(R.string.title_settings);
		bar.setHomeButtonEnabled(true);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);

	}

}
